<template>
  <div>
    <div>
      <el-row>
        <el-button type="primary" icon="Plus" @click="showItemTable"
          >新增
        </el-button>
        <el-button type="danger" icon="Delete" @click="handleDelete"
          >删除
        </el-button>
      </el-row>
    </div>
    <el-divider></el-divider>
    <div>
      <el-form :inline="true">
        <el-form-item label="门店名称">
          <el-input
            v-model="query.branchName"
            placeholder="请输入门店名称或编码"
            @keyup.enter="currentChange(1)"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="query.itemSaleName"
            placeholder="请输入商品名称或编码"
            @keyup.enter="currentChange(1)"
          ></el-input>
        </el-form-item>
        <el-form-item label="销售渠道">
          <el-select
            v-model="query.saleChannel"
            clearable
            placeholder="请选择"
            style="width: 100px"
          >
            <el-option
              v-for="item in saleChannelOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="currentChange(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        row-key="itemSaleChannelId"
        border
        default-expand-all
        stripe
        @select-all="handleAllSelectionItemSale"
        @select="handleSelectionItemSale"
      >
        <el-table-column type="selection"></el-table-column>
        <el-table-column type="index" label="序号" width="60">
        </el-table-column>
        <el-table-column prop="saleChannel" label="销售渠道">
          <template #default="scope">
            <el-tag
              :type="getSaleChannelTextTag(scope.row.saleChannel)"
              disable-transitions
              >{{ getSaleChannelText(scope.row.saleChannel) }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="branchCode" label="门店编码"> </el-table-column>
        <el-table-column prop="branchName" label="门店名称"> </el-table-column>
        <el-table-column prop="itemSaleCode" label="销售商品编码">
        </el-table-column>
        <el-table-column
          prop="itemSaleName"
          label="销售商品名称"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="itemSaleChannelName"
          label="渠道商品名称"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              size="small"
              icon="Edit"
              @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" class="row-bg" justify="end">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.current"
          :page-size="query.size"
          :total="query.total"
          @current-change="currentChange"
        >
        </el-pagination>
      </el-row>
    </div>

    <ItemSaleChannelTable
      ref="itemSaleChannelTable"
      @onClick="handleClickItemSale"
    ></ItemSaleChannelTable>

    <el-dialog title="修改渠道商品" v-model="showEdit" width="30%">
      <el-form :model="form">
        <el-form-item label="渠道商品名称">
          <el-input
            v-model="form.itemSaleChannelName"
            autocomplete="off"
            placeholder="请输入渠道商品名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleUpdate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Branch">
import {
  pageItemSaleChannel,
  addItemSaleChannel,
  updateItemSaleChannel,
  deleteItemSaleChannel,
} from "@/api/itemSaleChannel";
import { ElMessage } from "element-plus";
import ItemSaleChannelTable from "@/components/common/ItemSaleChannelTable.vue";
import { ref, onMounted } from "vue";

let itemSaleChannelTable = ref();
let showEdit = ref(false);
let query = ref({ size: 10, current: 0, total: 1 });
let form = ref({ branchStatus: 2 });
let tableData = ref([]);
let saleChannelOptions = ref([
  {
    value: 1,
    label: "小程序",
  },
  {
    value: 2,
    label: "美团",
  },
  {
    value: 3,
    label: "饿了么",
  },
]);
let selectionItemSaleChannels = ref([]);

function getSaleChannelTextTag(saleChannel) {
  let tagType = "info";
  if (saleChannel == 1) {
    tagType = "success";
  } else if (saleChannel == 2) {
    tagType = "warning";
  } else if (saleChannel == 3) {
    tagType = "primary";
  }
  return tagType;
}
function getSaleChannelText(saleChannel) {
  let channel = saleChannelOptions.value.filter(
    (channel) => channel.value == saleChannel
  );
  if (channel) {
    return channel[0].label;
  }
  return "";
}

function currentChange(current) {
  query.value.current = current;
  loadData();
}
function loadData() {
  pageItemSaleChannel(query.value)
    .then((res) => {
      query.value.size = res.data.size;
      query.value.current = res.data.current;
      query.value.total = res.data.total;
      tableData.value = res.data.records;
    })
    .catch((err) => {
      console.log(err);
    });
}
function resetForm() {
  form.value = {};
}
function showEditDialog(row) {
  showEdit.value = true;
  form.value = JSON.parse(JSON.stringify(row));
}
function showItemTable() {
  itemSaleChannelTable.value.dialogTableVisible = true;
}
function handleUpdate() {
  updateItemSaleChannel({
    itemSaleChannelId: form.value.itemSaleChannelId,
    itemSaleChannelName: form.value.itemSaleChannelName,
  })
    .then((res) => {
      showEdit.value = false;
      resetForm();
      ElMessage.success("修改成功");
      loadData();
    })
    .catch((err) => {
      console.log(err);
    });
}
function handleClickItemSale(itemSaleChannelTableData) {
  itemSaleChannelTable.value.dialogTableVisible = false;
  let branchIds = itemSaleChannelTableData.selectionBranches.map(
    (m) => m.sysBranchId
  );
  let itemSaleIds = itemSaleChannelTableData.selectionItemSales.map(
    (m) => m.itemSaleId
  );
  addItemSaleChannel({
    saleChannels: itemSaleChannelTableData.checkedChannels,
    sysBranchIds: branchIds,
    itemSaleIds: itemSaleIds,
  })
    .then((res) => {
      resetForm();
      ElMessage.success("保存成功");
      loadData();
    })
    .catch((err) => {
      console.log(err);
    });
}
function handleDelete() {
  let ids = selectionItemSaleChannels.value
    .map((itemSaleChannel) => itemSaleChannel.itemSaleChannelId)
    .join(",");
  deleteItemSaleChannel(ids)
    .then((res) => {
      resetForm();
      ElMessage.success("删除成功");
      loadData();
    })
    .catch((err) => {
      console.log(err);
    });
}
function handleAllSelectionItemSale(selection) {
  selectionItemSaleChannels.value = selection;
}
function handleSelectionItemSale(selection, row) {
  selectionItemSaleChannels.value = selection;
}

onMounted(() => {
  loadData();
});
</script>

<style>
table {
  width: 100% !important;
}
</style>
